// scalaxx - Scala XML eXtensions
// (c) 2009-2010, Normen Müller, normen.mueller@gmail.com
// $Id: Commands.scala 84 2010-09-26 16:02:32Z normen.mueller $
package scalaxx.diff

import scala.xml.{Node, MetaData}

import scalaxx.path._

/** Edit script commands following the example of XPath 1.0.
 * 
 *  Note: At current state, command paths are restricted to XPath 1.0. location paths.
 * 
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
sealed abstract class Command {
  def prefix: String
  
  val path: LocationPath
  
  val instruction: Seq[Instruction]
  
  override def toString(): String = prefix+"("+path+", " + (instruction mkString) + ")"
}

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class append(val path: LocationPath, val instruction: Instruction*) extends Command { def prefix: String = "append" }

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class remove(val path: LocationPath, val instruction: Instruction*) extends Command { def prefix: String = "remove" }

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class update(val path: LocationPath, val instruction: Instruction*) extends Command { def prefix: String = "update" }

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class before(val path: LocationPath, val instruction: Instruction*) extends Command { def prefix: String = "insert-before" }

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
abstract class Instruction

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class elem(e: Seq[Node]) extends Instruction { override def toString(): String = e mkString }

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class attr(e: MetaData) extends Instruction  { override def toString(): String = e mkString }

/// DRAFT

/**
 *  @draft for fine granular change detection within XML text nodes
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class patch(val path: LocationPath, val instruction: Instruction*) extends Command { 
  def prefix: String = "patch"
  
  override def toString(): String = prefix+"("+path+", \n" + (instruction mkString) + "\n)"
}

/**
 *  @draft for fine granular change detection within XML text nodes
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
case class delta(d: String) extends Instruction { override def toString(): String = d mkString }
